package com.khotyn.heresy.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.khotyn.heresy.bean.Collection;
import com.khotyn.heresy.bean.PictureBrief;
import com.khotyn.heresy.bean.PictureDetail;
import com.khotyn.heresy.dao.CollectionDAO;
import com.khotyn.heresy.dao.FriendDAO;
import com.khotyn.heresy.dao.PictureDAO;
import com.khotyn.heresy.exception.NullCollectionException;
import com.khotyn.heresy.exception.NullPictureException;
import com.khotyn.heresy.exception.PermissionException;
import com.khotyn.heresy.exception.ReCollectException;

/**
 * 进行收藏操作的服务类
 * @author 金灵杰
 *
 */
@Service
public class FavouriteService {
	@Autowired
	private PictureDAO pictureDAO;

	@Autowired
	private FriendDAO friendDAO;
	
	@Autowired
	private CollectionDAO collectionDAO;
	/**
	 * @param pictureDAO the pictureDAO to set
	 */
	public void setPictureDAO(PictureDAO pictureDAO) {
		this.pictureDAO = pictureDAO;
	}

	/**
	 * @param friendDAO the friendDAO to set
	 */
	public void setFriendDAO(FriendDAO friendDAO) {
		this.friendDAO = friendDAO;
	}

	/**
	 * @param collectionDAO the collectionDAO to set
	 */
	public void setCollectionDAO(CollectionDAO collectionDAO) {
		this.collectionDAO = collectionDAO;
	}

	/**
	 * 添加收藏服务，将图片添加为收藏
	 * @param uid 用户id
	 * @param pid 图片id
	 * @throws NullPictureException 指定图片不存在时抛出
	 * @throws PermissionException 用户没有权限访问该图片时抛出
	 * @throws ReCollectException 图片已经收藏
	 */
	public void addFavouriteSerivce(int uid, String pid) throws NullPictureException, PermissionException, ReCollectException
	{
		if(pid == null)
		{
			throw new NullPictureException();
		}
		PictureDetail picDetail = pictureDAO.selectPicByPicID(pid);
		if(picDetail == null)		//图片不存在
		{
			throw new NullPictureException();
		}
		
		//图片限制是私有，或者图片限制是好友，但用户不是图片主人的好友,或图片已经被标记删除
		if(picDetail.getFlagLimit().equalsIgnoreCase("private") 
				|| (picDetail.getFlagLimit().equalsIgnoreCase("friend") 
						&& friendDAO.selectOneFriend(picDetail.getPicOwner().getUserID(), uid) == null)
				|| (picDetail.getFlagDelete().equalsIgnoreCase("yes")))	
		{
			throw new PermissionException();
		}
		
		List<Collection> collectionList = collectionDAO.selectCollectionByUserId(uid);
		for (Collection collection : collectionList) {
			if(pid.equalsIgnoreCase(collection.getPictureBrief().getPictureID()))
			{
				throw new ReCollectException();
			}
		}
		
		//将收藏信息放入数据库
		//FIXED:修改picBrief的函数，获取的为缩略图
		//FIXME:加入事务
		pictureDAO.updateCollectCount(picDetail.getPictureID(), picDetail.getCollectCount() + 1);
		
		PictureBrief picBrief = new PictureBrief();
		picBrief.setThumbLocation(picDetail.getThumbLocation());
		picBrief.setName(picDetail.getName());
		picBrief.setOwner(picDetail.getPicOwner());
		picBrief.setPictureID(picDetail.getPictureID());
		
		Collection collection = new Collection();
		collection.setPictureBrief(picBrief);
		collection.setUserID(uid);
		
		collectionDAO.insertCollection(collection);
	}
	
	/**
	 * 删除收藏服务，将收藏的图片删除
	 * @param uid 用户id
	 * @param cid 收藏id
	 * @throws NullCollectionException 收藏不存在时抛出
	 */
	public void delFavouriteService(int uid, String cid) throws NullCollectionException
	{
		int CID = Integer.parseInt(cid);
		boolean foundCollection = false;
		//FIXME:添加事务
		List<Collection> collectionList = collectionDAO.selectCollectionByUserId(uid);
		for (Collection collection : collectionList) {
			if(collection.getCollectionID() == CID)
			{
				PictureDetail picDetail = pictureDAO.selectPicByPicID(collection.getPictureBrief().getPictureID());
				pictureDAO.updateCollectCount(picDetail.getPictureID(), picDetail.getCollectCount() - 1);
				foundCollection = true;
				break;
			}
		}
		
		if(!foundCollection)
		{
			throw new NullCollectionException();
		}
		
		int affectedRows = collectionDAO.deleteCollectionById(CID);
		if(affectedRows == 0)	//没有删除，收藏不存在
		{
			throw new NullCollectionException();
		}
	}
}
